import java.awt.*;
import java.util.*;
import java.text.*;


public class Category
{
    private String name;
    private ArrayList<Gesture> prototypes;

    public Category(String name)
    {
        this.name = name;
        this.prototypes = null;
    }

    public Category(String name, Gesture fExample)
    {
        this.name = name;
        this.prototypes = new ArrayList<Gesture>();
        this.addExample(fExample);
    }

    public Category(String name, ArrayList<Gesture> examples)
    {
        this.name = name;
        this.prototypes = new ArrayList<Gesture>(examples.size());
        for (Gesture o : examples)
        {
            this.addExample(o);
        }
    }

    public String getName()
    {
        return this.name;
    }

    public int numExamples()
    {
        return this.prototypes.size();
    }

    public Gesture getGesture(int index)
    {
        if (index >= 0 && index < this.numExamples())
        {
            return (Gesture) this.prototypes.get(index);
        }
        else
        {
            return null;
        }
    }

    public void addExample(Gesture g)
    {
        boolean success = true;
        try
        {
            String pname = Category.parseName(g.getName());
            if (!pname.equals(this.name))
            {
                throw new java.lang.IllegalArgumentException(
                        "Prototype name does not equal the name of the category to which it was added.");
            }

            for (Object o : this.prototypes)
            {
                Gesture g0 = (Gesture) o;
                if (!g0.getName().equals(g.getName()))
                {
                    throw new java.lang.IllegalArgumentException(
                            "Prototype name was added more than once to its category.");
                }
            }
        } catch (java.lang.IllegalArgumentException e)
        {
            System.err.println(e);
            success = false;
        }
        if (success)
        {
            this.prototypes.add(g);
        }
    }

    public static String parseName(String s)
    {
        String category = "";
        for (int i = s.length() - 1; i >= 0; i--)
        {
            if (!Character.isDigit(s.charAt(i)))
            {
                category = s.substring(0, i + 1);
            }
        }
        return category;
    }
}
